//
//  HMApplyForGoodsViewController.m
//  HMMarket
//
//  Created by haohao on 2017/6/26.
//  Copyright © 2017年 homai. All rights reserved.
//

#define rowHeight 49
#define marin 20
#import "HMApplyForGoodsViewController.h"
#import "HMSelectApplyGoodTableViewCell.h"
#import "HMApplyMoneyTableViewCell.h"
#import "HMApplyFhotoTableViewCell.h"
#import "HMApplyReasonTableViewCell.h"
#import "HMSelectApplyGoodTableViewCell.h"
#import "OrderModel.h"
#import "ChooseGoodModel.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "HMReturnGoodViewController.h"
@interface HMApplyForGoodsViewController ()<UITableViewDelegate, UITableViewDataSource, HMSelectApplyGoodTableViewCell, HMApplyReasonTableViewCellDelegate, HMApplyFhotoTableViewCellDelegate>

NS_STRONG UITableView *myTableView;
NS_ASSIGN float returnPrice;

NS_ASSIGN float thirdRowHeight;

NS_COPY   NSString *thirdString;

NS_STRONG NSMutableArray *imageArray;

NS_STRONG NSArray *goodArray;

@end

@implementation HMApplyForGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubView];
}

- (void)setUpSubView {
    self.title = @"申请退货";
    _thirdRowHeight = rowHeight;
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, KWidthSCREEN, KHeightSCREEN - 48 - 64) style:UITableViewStyleGrouped];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.separatorColor = [UIColor clearColor];
    _myTableView.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0];
    [self.view addSubview:_myTableView];
    
    UIButton *submit = [UIButton buttonWithType:UIButtonTypeCustom];
    [submit setTitle:@"提交申请" forState:UIControlStateNormal];
    submit.titleLabel.font = [UIFont systemFontOfSize:16.0];
    submit.frame = CGRectMake(0, KHeightSCREEN - 48, KWidthSCREEN, 48);
    [submit setBackgroundColor:kMainColor];
    [submit addTarget:self action:@selector(returnGoodS) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submit];
    
    [self.myTableView registerNib:[UINib nibWithNibName:@"HMSelectApplyGoodTableViewCell" bundle:nil] forCellReuseIdentifier:@"HMSelectApplyGoodTableViewCell"];
}

- (void)returnGoodS {
    if (_goodArray.count == 0) {
        [HMHUDManager showHUDWithText:@"没有可退货的商品"];
        return;
    }
    
    NSString *itemidString;
    for (int i = 0; i < _goodArray.count; i++) {
        if (i == 0) {
            ChooseGoodModel *model = _goodArray[i];
            itemidString = model.item_id;
        } else {
            ChooseGoodModel *model = _goodArray[i];
            itemidString = [NSString stringWithFormat:@"%@,%@", itemidString, model.item_id];
        }
    }
    NSDictionary *parm = @{@"oid":_model.oid, @"itemid":[HMReductionData replaceNullValue:itemidString],
                           @"price":[NSString stringWithFormat:@"%.2f", _returnPrice], @"content":[HMReductionData replaceNullValue:_thirdString]};
    
    [HttpBaseTool post:kOrderReturn params:parm imageParm:@"pic[]" images:_imageArray imagesName:@[@"pic1.jpg", @"pic2.jpg", @"pic3.jpg"] success:^(id responseObj) {
        if ([responseObj[@"code"] integerValue] == 200) {
            [HMHUDManager showHUDWithText:@"申请成功，请耐心等待客服处理"];
            OrderRefresh;
//            HMReturnGoodViewController *vc = [[HMReturnGoodViewController alloc] init];
//            vc.returnPrice = _returnPrice;
//            vc.thirdRowHeight = _thirdRowHeight;
//            vc.thirdString = _thirdString;
//            vc.imageArray = _imageArray;
//            vc.goodArray = _goodArray;
//            vc.model = _model;
//            vc.oid = responseObj[@"data"][@"oid"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [HMHUDManager showHUDWithText:responseObj[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HMSelectApplyGoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMSelectApplyGoodTableViewCell" forIndexPath:indexPath];
        cell.id = _model.oid;
        cell.delegate = self;
        return cell;
    } else if (indexPath.section == 1) {
        HMApplyMoneyTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HMApplyMoneyTableViewCell" owner:self options:nil] firstObject];
        cell.totalMoney = _model.total_price;
        cell.returnMoney = [NSString stringWithFormat:@"%.2f", _returnPrice];
        return cell;
    } else if (indexPath.section == 2) {
        static NSString *identifi = @"HMApplyReasonTableViewCell";
         HMApplyReasonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifi];
        if (cell == nil) {
            cell = [[HMApplyReasonTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifi];
            cell.delegate = self;
        }
        cell.text = _thirdString;
        return cell;
    } else if (indexPath.section == 3) {
        HMApplyFhotoTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HMApplyFhotoTableViewCell" owner:self options:nil] firstObject];
        cell.delegate = self;
        return cell;
    } else {
        return nil;
    }
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        return _thirdRowHeight;
    } else if (indexPath.section == 3) {
        return 60;
    } else {
        return rowHeight;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidthSCREEN, rowHeight)];
    
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(marin, 0, KWidthSCREEN - marin, rowHeight)];
    [titleView addSubview:headerLabel];
    
//    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
    if (section == 0) {
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"*退货商品"];
        [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
        headerLabel.attributedText = AttributedStr;
        return titleView;
    } else if (section == 2){
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"*退货原因"];
        [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
        headerLabel.attributedText = AttributedStr;
        return titleView;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return rowHeight;

    } else if (section == 2){
        return rowHeight;
    } else {
        return 10;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00001;
}
#pragma mark - HMSelectApplyGoodTableViewCell
- (void)returnGoodPrice:(float)price goodArray:(NSArray *)goodArray{
    _goodArray = goodArray;
    if (price > [_model.total_price floatValue]) {
        _returnPrice = [_model.total_price floatValue];
    } else {
        _returnPrice = price;
    }
    [_myTableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
}
#pragma mark - HMApplyReasonTableViewCellDelegate
- (void)returnRowHeight:(float)height textView:(NSString *)text{
    MyLog(@"height = %f", height);
    
    _thirdRowHeight = height;
    _thirdString = text;
    [self.myTableView.delegate tableView:_myTableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]];
    [self.myTableView beginUpdates];
    [self.myTableView endUpdates];
}
#pragma mark - HMApplyFhotoTableViewCellDelegate
- (void)selectPhoto:(NSMutableArray *)dataArray image:(NSMutableArray *)imageArray {
    [self.imageArray removeAllObjects];
    [self.imageArray addObjectsFromArray:imageArray];
    for (int i = 0; i < dataArray.count; i++) {
        ALAsset *assetData = dataArray[i];
        ALAssetRepresentation* representation = [assetData defaultRepresentation];
        UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage]];
        [self.imageArray addObject:image];
    }
}
- (NSMutableArray *)imageArray {
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

@end
